#include "Enemy.h"

#include "InputHandler.h"
#include "Utility.h"
#include "GameGlobals.h"
#include "Resources.h"

LINKCLASSTONAME("ent_enemy", Enemy);

Enemy::Enemy(sf::RenderWindow *App) : BaseMoveable(App)
{
	sf::Vector2f Points[4];
	float height, width;
	height = 99;
	width = 54;
	Points[0] = sf::Vector2f(-width / 2,height / 2);
	Points[1] = sf::Vector2f(width / 2, height / 2);
	Points[2] = sf::Vector2f(width / 2, -height / 2);
	Points[3] = sf::Vector2f(-width / 2, -height / 2);
	CreatePhysics();
	GetPhysicsObject()->PhysicsInitPolygon(this, Points, 4);
	GetPhysicsObject()->SetMass(250);

	std::vector<BaseEntity*>Players;
	EntityManager::Instance()->FindByClass("ent_player", Players);
	mTarget = Players.front();
	SetHealth(100);
	float RandCircle = Util::Random(0.f,1.f);
	sf::Vector2<float> Pos(std::cos(RandCircle * 6.28f) * 800.f, std::sin(RandCircle * 6.28f) * 800.f);
	Pos += mTarget->GetPos();
	SetPos(Pos);

	Sprite.SetTexture(*Resources::Instance()->RequestImage("EnemyBody", true));
	Sprite.SetOrigin(Sprite.GetSize().x / 2, Sprite.GetSize().y / 2);
	TankSpeed = 150.f;

	mTurret = EntityManager::Instance()->CreateEntity("ent_tankturret");
	mTurret->Fire(std::string("setenemy"));
	mTurret->SetPos(GetPos());
	mTurret->SetOwner(this);

	Speed = 0.f;
	NextUpdatePos = 0.f;

	MinRange = Util::Random(50,300);
}

Enemy::~Enemy(void)
{
}

void Enemy::OnTakeDamage(BaseEntity* inf, float Amount)
{
	if (inf->GetClass() == std::string("ent_shell") )
	{
		AddHealth(-Amount);
	}
}

void Enemy::OnCollide(BaseMoveable *ent1, BaseMoveable *ent2, CollisionData CollideInfo)
{
}

void Enemy::Think()
{
	if (GetHealth() <=0)
	{
		Explode();
		return;
	}

	float delta = GameGlobals::Instance()->FrameTime;

	float TurretRot = -Util::Angle(mTarget->GetPos(), GetPos()) + 90.f;
	mTurret->SetAngle(Util::RotateTowards(mTurret->GetAngle(), TurretRot, 2.f));
	
	float rot = GetAngle();
	double x,y;
	x = sin(Util::DegToRad(rot));
	y = cos(Util::DegToRad(rot));

	float TargetDist = Util::Distance(GetPos(), mTarget->GetPos());

	if (TargetDist > MinRange)
		GetPhysicsObject()->SetVelocity(sf::Vector2f(Speed * x, Speed * y));

	float ang = -Util::Angle(TargetPos, GetPhysicsObject()->GetPos()) + 90.f;
	if (NextUpdatePos < GameGlobals::Instance()->CurTime)
	{
		TargetPos = mTarget->GetPos() + Util::VecRand(-50,50);
		NextUpdatePos = GameGlobals::Instance()->CurTime + Util::Random(10.f, 15.f); 
	}

	SetAngle(Util::RotateTowards(GetAngle(), ang, Util::Length(GetPhysicsObject()->GetVelocity()) / 25.f));

	float aimangle = Util::NormalizeAngle(-Util::Angle(mTarget->GetPos(), GetPhysicsObject()->GetPos()) + 90.f);
	if (std::abs(mTurret->GetAngle() - aimangle) < 25 && TargetDist < 500.f)
	{
		mTurret->Fire(std::string("shoot"));
	}


	Speed = 25;
}

void Enemy::Explode()
{
	if (Util::Random(1,3) == 1)
	{
		BaseEntity* health = EntityManager::Instance()->CreateEntity("ent_healthpack");
		BaseMoveable* health2 = dynamic_cast<BaseMoveable*>(health);
		health2->SetPos(GetPhysicsObject()->GetPos());
	}

	BaseEntity* b = EntityManager::Instance()->CreateEntity("ent_decal");
	b->SetPos(GetPos());
	b->SetAngle(Util::Random(0,360));
	Delete();
}

void Enemy::Draw()
{
	Sprite.SetPosition(GetSFMLPos());
	Sprite.SetRotation(GetAngle());
	mTurret->SetPos(GetPos());
	pRender->Draw(Sprite);
	GetPhysicsObject()->Draw();
}

void Enemy::OnDelete()
{
	mTurret->Delete();
}
